package com.wss.common.wechat.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.google.gson.Gson;
import com.wss.common.wechat.cache.WeChatCache;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author 阿飞
 *	用于微信相关令牌的授权
 */
@Controller
public class ForeignAuthorizedController {
	
    Logger logger = Logger.getLogger(ForeignAuthorizedController.class);


    @ResponseBody
    @RequestMapping(value = "/wechat/foreignAuthAccessToken.do",method = RequestMethod.POST,produces = { "application/json;charset=UTF-8" })
    public String foreignAuthAccessToken(HttpServletRequest request){
        //未前端JS-SDK做必要的准备工作
        Gson gson = new Gson();
        Map<String,Object> reMap = new HashMap<String,Object>();
        logger.info("当前访问接口的IP为："+getRemoteHost(request));
		//增加IP校验 112.124.29.152
        if(!getRemoteHost(request).contains("1")){
        	reMap.put("errcode", 40001);
        	reMap.put("errmsg", "this ip is not permiss");
            return gson.toJson(reMap);
        }
        reMap.put("errcode", 0);
    	reMap.put("errmsg", "ok");
        reMap.put("api_component_token", WeChatCache.getApiComponentToken());
        reMap.put("expires_in", 60);
        String jsonStr= gson.toJson(reMap);
        logger.info("returns API_COMPONENT_TOKEN json["+jsonStr+"]");
        return jsonStr;
    }
    
//    @ResponseBody
//    @RequestMapping(value = "/wechat/foreignAuthJsapiTicket.do",method = RequestMethod.POST,produces = { "application/json;charset=UTF-8" })
//    public String foreignAuthJsapiTicket(HttpServletRequest request){
//        //未前端JS-SDK做必要的准备工作
//        Map<String,Object> reMap = new HashMap<String,Object>();
//        logger.info("当前访问接口的IP为："+getRemoteHost(request));
//        if(!getRemoteHost(request).contains("112.124.29.152")){
//        	reMap.put("errcode", 40001);
//        	reMap.put("errmsg", "this ip is not permiss");
//            return JSONObject.fromObject(reMap).toString();
//        }
//        reMap.put("errcode", 0);
//    	reMap.put("errmsg", "ok");
//        reMap.put("expires_in", 60);
//        reMap.put("jsapi_ticket", JsapiTicketUtil.getJsapiTicket());
//        String jsonStr=JSONObject.fromObject(reMap).toString();
//        logger.info(TierConst.WEIXIN+"returns jsticket json["+jsonStr+"]");
//        return jsonStr;
//    }
    
    public String getRemoteHost(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
    }
    public static void main(String[] args) {
		System.out.println("112.124.29.152, 112.124.29.152, 192.168.0.10".contains("112.124.29.152"));
		System.out.println("112.124.29.152, 112.124.29.152, 192.168.0.10".contains("112.124.29.152"));
	}
}
